﻿using System;
using System.Collections.Generic;
using BSpec.Conventions;

namespace BSpec.Specification
{
    public class Scenario : SpecificationElement
    {
        private readonly List<Context> _contexts = new List<Context>();

        public Scenario(string name) 
            : base(name)
        {
        }

        public Context[] Contexts
        {
            get { return _contexts.ToArray(); }
        }

        public void AddContextFor(Type testFixtureType)
        {
            var context = Context.Create(testFixtureType);

            if (_contexts.HasElement(context))
            {
                _contexts.GetElement(context).AddBehaviourFor(testFixtureType);
            }
            else
            {
                context.AddBehaviourFor(testFixtureType);
                _contexts.Add(context);
            }
        }

        public static Scenario Create(Type testFixtureType)
        {
            string name = testFixtureType.BaseType.GetName(NamingConvention.Scenario);

            return name.IsEmpty() ? new NullScenario() : new Scenario(name);
        }
    }

    public class NullScenario : Scenario
    {
        public NullScenario()
            : base(string.Empty)
        {
        }
    }
}